//package com.qmyx.supplychain.quartz.task;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.ytjj.qmyx.supplychain.common.model.dto.OrdersFreightDto;
//import com.ytjj.qmyx.supplychain.common.utils.DingDingWebHookUtil;
//import com.ytjj.qmyx.supplychain.common.utils.RedisService;
//import com.ytjj.qmyx.supplychain.mapper.AssignFreightMapper;
//import com.ytjj.qmyx.supplychain.mapper.entity.YxAssignFreight;
//import com.ytjj.qmyx.supplychain.service.OrderFreightService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.context.config.annotation.RefreshScope;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//import tk.mybatis.mapper.entity.Example;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.time.LocalDate;
//import java.time.format.DateTimeFormatter;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 订单运费检查
// */
//@Component
//@Slf4j
//@RefreshScope
//public class OrderFreightCheckTask {
//    @Autowired
//    private RedisService redisService;
//    @Autowired
//    private OrderFreightService orderFreightService;
//    @Resource
//    private AssignFreightMapper assignFreightMapper;
//    /**
//     * 消息模板
//     */
//    private static final String template = "<<<%s>>> \n 【服务名】: %s(%s) \n 【状态】: %s(%s) \n 【服务ip】: %s \n 【详情】: %s";
//
//    /**
//     * 检查订单运费异常数据
//     */
//    @Scheduled(cron = "0 0/5 * * * ?")
//    public void checkOrderFreightError() {
//        log.info("===运费警告任务start===");
//        String batch_task_finish = redisService.get("batch_task_finish");
//        if (!"1".equals(batch_task_finish)) {
//            return;
//        }
//        List<String> differenceOrderNos = new ArrayList<>();
//        try {
//            LocalDate localDate = LocalDate.now().plusDays(-1);
//            String startTime = localDate.format(DateTimeFormatter.ISO_DATE) + " 14:00:00";
//            List<OrdersFreightDto> datas = orderFreightService.queryOrderFreightInfo(startTime);
//            datas.stream().forEach(item -> {
//                Example example = new Example(YxAssignFreight.class);
//                example.createCriteria()
//                        .andEqualTo("freightId", item.getFreightId())
//                        .andLike("region", item.getCity());
//                example.orderBy("id").desc();
//                YxAssignFreight assignFreight = assignFreightMapper.selectOneByExample(example);
//                if (null == assignFreight && item.getFreight().compareTo(new BigDecimal("0")) <= 0) {
//                    return;
//                }
//                if (null != assignFreight && assignFreight.getFreight().compareTo(item.getFreight()) != 0) {
//                    differenceOrderNos.add(item.getChildOrderNo());
//                }
//            });
//        } finally {
//            redisService.set("batch_task_finish", "0");
//        }
//        if (!CollectionUtils.isEmpty(differenceOrderNos)) {
//            String messageText = String
//                    .format(template, "运费异常告警",
//                            "supplychain-quartz", "order.freight_error",
//                            "500", "存在运费异常订单",
//                            "",
//                            JSONObject.toJSONString("存在运费异常订单，子订单号：" + JSONObject.toJSONString(differenceOrderNos)));
//            DingDingWebHookUtil.pushInfoToDingding(messageText, null);
//        }
//        log.info("===运费警告任务end===");
//    }
//}
